package com.ruoyi.medicine.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.medicine.domain.HisMedicineCart;
import com.ruoyi.medicine.domain.HisMedicineDrug;
import com.ruoyi.medicine.mapper.HisMedicineCartMapper;
import com.ruoyi.medicine.mapper.HisMedicineDrugMapper;
import com.ruoyi.medicine.service.iHisMedicineCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @program: RuoYi-Vue-master
 * @ClassName HisMedicineCartServiceimpl
 * @description:
 * @author: xiaoJie
 * @create: 2024-09-01 14:18
 * @Version 1.0
 */
@Service
public class HisMedicineCartServiceImpl implements iHisMedicineCartService {

    @Autowired
    private HisMedicineCartMapper hisMedicineCartMapper;
    @Autowired
    private HisMedicineDrugMapper hisMedicineDrugMapper;

    //删除购物车药品
    @Override
    @Transactional
    public int deleteMedicineCartByDrugId(Long[] drugId,Long userId) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        HisMedicineCart hisMedicineCart = hisMedicineCartMapper.selectMedicineCartId(userId);
        String cart = hisMedicineCart.getCart();
        Map<Long, HisMedicineDrug> map = objectMapper.readValue(cart, new TypeReference<Map<Long, HisMedicineDrug>>() {});
        Arrays.stream(drugId).forEach(o ->map.remove(o));
        String s = objectMapper.writeValueAsString(map);
        hisMedicineCart.setCart(s);
        int i = hisMedicineCartMapper.updateMedicineCart(hisMedicineCart);
        return i;
    }

    //修改数量
    @Override
    @Transactional(propagation =  Propagation.REQUIRED,rollbackFor = Exception.class)
    public int update(Long userId, Long drugId, Integer num) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        //通过采购人id查找购物车
        HisMedicineCart hisMedicineCart = hisMedicineCartMapper.selectMedicineCartId(userId);
        //获取购物车药品
        String cart = hisMedicineCart.getCart();
        //把json转换成相应键和值 对象
        Map<Long, HisMedicineDrug> map = objectMapper.readValue(cart, new TypeReference<Map<Long, HisMedicineDrug>>() {});
        //通过map的键查找相应的值对象
        HisMedicineDrug hisMedicineDrug = map.get(drugId);
        //把修改后的数量重新覆盖给对象
        hisMedicineDrug.setCarNum(num);
        //在把map转成json
        String s = objectMapper.writeValueAsString(map);
        //存放到购物车
        hisMedicineCart.setCart(s);
        //修改
        int i = hisMedicineCartMapper.updateMedicineCart(hisMedicineCart);
        return i;
    }

    @Override
    public List<HisMedicineDrug> selectCarByUserId(Long userId) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        //查询采购人的购物车
        HisMedicineCart hisMedicineCart = hisMedicineCartMapper.selectMedicineCartId(userId);
        String cart = hisMedicineCart.getCart();
        Map<Long, HisMedicineDrug> map = objectMapper.readValue(cart, new TypeReference<Map<Long, HisMedicineDrug>>() {});
        Collection<HisMedicineDrug> values = map.values();
        List<HisMedicineDrug> list = new ArrayList<>(values);
        return list;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int saveCat(Long userId, Long[] drugIds) throws JsonProcessingException {
        Map<Long, HisMedicineDrug> map;
        ObjectMapper objectMapper = new ObjectMapper();
        //判断用户是否有购物车
        HisMedicineCart hisMedicineCarts = hisMedicineCartMapper.selectMedicineCartId(userId);
        if (hisMedicineCarts == null) {
            //用户还未存在购物车
            map = new HashMap<>();
        } else {
            String cart = hisMedicineCarts.getCart();
            map = objectMapper.readValue(cart, new TypeReference<Map<Long, HisMedicineDrug>>() {});
        }
        //判断传过来的药品id是否在购物车存在
        for (Long drugId : drugIds) {
            HisMedicineDrug hisMedicineDrug = map.get(drugId);
            if (hisMedicineDrug == null) {
                //药品不存在时
                HisMedicineDrug hisMedicineDrug1 = hisMedicineDrugMapper.selectHisMedicineDrugByDrugId(drugId);
                hisMedicineDrug1.setCarNum(1);
                map.put(drugId, hisMedicineDrug1);
            }else {
                hisMedicineDrug.setCarNum(hisMedicineDrug.getCarNum()+1);
                map.put(drugId,hisMedicineDrug);
            }
        }
        //转换成json存储到数据库
        String s = objectMapper.writeValueAsString(map);
        if (hisMedicineCarts == null){
            HisMedicineCart hisMedicineCart = new HisMedicineCart();
            hisMedicineCart.setCart(s);
            hisMedicineCart.setUserId(userId);
            return hisMedicineCartMapper.insetMedicineCart(hisMedicineCart);
        }else {
            hisMedicineCarts.setCart(s);
            return hisMedicineCartMapper.updateMedicineCart(hisMedicineCarts);
        }
    }
}
